/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";

$.ajax({
    url: HOST_ADDR + '/cpmp/search/company-all',
    type: 'post',
    data: {},
    dataType: 'json',
    contentType:"application/json",
    success: function(resp){
        if(resp.length){
            console.log(resp);
            for(var i = 0; i<resp.length;i++){
                jQuery('.itdcCompy').append('<option value="'+resp[i].dept_name+'" data-id="'+resp[i].id+'" data-deptCode="'+resp[i].dept_code+'">'+resp[i].dept_name+'</option>');
            }
        }
    },
    error:function(err){
        console.log(err);
    }
})



var Login = function () {

    return {
        //main function to initiate the module
        init: function () {

            $('.login-form').validate({
                errorElement: 'label', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                rules: {
                    username: {
                        required: true
                    },
                    password: {
                        required: true
                    },
                    remember: {
                        required: false
                    }
                },

                messages: {
                    username: {
                        required: "请填写用户名"
                    },
                    password: {
                        required: "请填写密码"
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit
                    $('.alert-error', $('.login-form')).show();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.control-group').addClass('error'); // set error class to the control group
                },

                success: function (label) {
                    label.closest('.control-group').removeClass('error');
                    label.remove();
                },

                errorPlacement: function (error, element) {
                    error.addClass('help-small no-left-padding').insertAfter(element.closest('.input-icon'));
                },

                submitHandler: function (form) {
                    let $form = $(form);
                    let username = $form.find('.username')[0].value;
                    let password = $form.find('.password')[0].value;
                    let data = {
                        "mim": password,
                        "usrNam": username
                    }
                    $.ajax({
                        url: HOST_ADDR + '/cpmp/splrSlfMng/login',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType:"application/json",
                        success: function(resp){
                            if(resp.success){
                                console.log(resp.data);
                                sessionStorage.setItem('supplier_token',resp.data.access_token);
                                sessionStorage.setItem('username', username);
                                sessionStorage.setItem('acntTyp',resp.data.acntTyp);
                                sessionStorage.setItem('reseIndex', '00');
                                sessionStorage.setItem('realName', resp.data.realName);
                                sessionStorage.setItem('fullNam', resp.data.fullNam);
                                sessionStorage.setItem('splrId', resp.data.splrId);
                                console.log(sessionStorage);
                                // //页面跳转到index
                                location.href = '/supplier_self/index.html';
                            }
                            else{
                                alert(resp.msgInf);
                                window.location.reload();
                            }

                        },
                        error:function(err){
                            console.log(err);
                        }

                    })
                }
            });

            $('.login-form input').keypress(function (e) {
                if (e.which == 13) {
                    if ($('.login-form').validate().form()) {
                        // window.location.href = "index.html";
                    }
                    return false;
                }
            });


            jQuery('#back-btn').click(function () {
                jQuery('.login-form').show();
                jQuery('.forget-form').hide();
            });

            $('.register-form').validate({
                errorElement: 'label', //default input error message container
                errorClass: 'help-inline', // default input error message class
                focusInvalid: false, // do not focus the last invalid input
                ignore: "",
                rules: {
                    companyname: {
                        required: true
                    },
                    username: {
                        required: true
                    },
                    password: {
                        required: true
                    },
                    rpassword: {
                        required: true,
                        equalTo: "#register_password"
                    },
                    email: {
                        required: true,
                    }
                },

                messages: { // custom messages for radio buttons and checkboxes
                    companyname: {
                        required: "请填写公司名"
                    },
                    username: {
                        required: "请填写用户名"
                    },
                    password: {
                        required: "请填写密码"
                    },
                    rpassword: {
                        required: "请重复密码",
                        equalTo: "请重复密码"
                    },
                    email: {
                        required: "请填写邮箱，注意邮箱格式"
                    }
                },

                invalidHandler: function (event, validator) { //display error alert on form submit
                    $('.alert-error', $('.register-form')).show();
                },

                highlight: function (element) { // hightlight error inputs
                    $(element)
                        .closest('.control-group').addClass('error'); // set error class to the control group
                },

                success: function (label) {
                    label.closest('.control-group').removeClass('error');
                    label.remove();
                },

                submitHandler: function (form) {
                    let $form = $(form);
                    let companyname = $form.find('.companyname')[0].value;
                    let email = $form.find('.email')[0].value;
                    let mim = $form.find('.password')[0].value;
                    let phone = $form.find('.phone')[0].value;
                    let userNam = $form.find('.username')[0].value;
                    let itdcCompy = $form.find('.itdcCompy')[0].value;
                    var data = {
                        "compangNam": companyname,
                        "email": email,
                        "mim": mim,
                        "phone": phone,
                        "usrNam": userNam,
                        "itdcCompy":itdcCompy
                    }
                    console.log(data);
                    $.ajax({
                        url: HOST_ADDR + '/cpmp/splrSlfMng/checkName',
                        type: 'post',
                        data: JSON.stringify({"name":companyname}),
                        dataType: 'json',
                        contentType: "application/json",
                        success: function (resp) {
                            if(resp.success){// 查询成功，该供应商名称可用
                                $.ajax({
                                    url: HOST_ADDR + '/cpmp/splrSlfMng/rgst',
                                    type: 'post',
                                    data: JSON.stringify(data),
                                    dataType: 'json',
                                    contentType:"application/json",
                                    success: function(resp){
                                        if(resp.success){
                                            sessionStorage.setItem('supplier_token',resp.data.access_token);
                                            sessionStorage.setItem('fullNam', companyname);
                                            sessionStorage.setItem('username', userNam);
                                            sessionStorage.setItem('reseIndex', '00');
                                            alert('注册成功');
                                            $form.find('.control-group input').val('');
                                            location.href='index.html';
                                        }
                                        else{
                                            alert(resp.msgInf);
                                        }
                                    },
                                    error:function(err){
                                        console.log(err);
                                    }
                                })
                            }else{
                                alert(resp.msgInf);
                                var errmsg = resp.msgInf;
                                if(resp.data!=null && resp.data!=''){
                                    errmsg = resp.msgInf+":"+resp.data;
                                }
                                $(".error_msg").html(errmsg);
                            }
                        },
                        error:function(err){
                            console.log(err);
                        }
                    })
                }
            });

            jQuery('#register-btn').click(function () {
                jQuery('.login-form').hide();
                jQuery('.register-form').show();
            });

            jQuery('#register-back-btn').click(function () {
                jQuery('.login-form').show();
                jQuery('.register-form').hide();
            });
            // $('#register-submit-btn').click(()=>{
            //
            // });
        }

    };

}();

jQuery(document).ready(function() {

    App.init();

    Login.init();

});
